package com.JieCheng.controller;

import com.JieCheng.dao.model.Subject;
import com.JieCheng.dao.model.User;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import jxl.read.biff.BiffException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by Zhang on 2017/5/10.
 */
@RestController
public class ServiceController extends BaseController{

    @ApiOperation(value="修改密码", notes="使用者可根据原密码修改至新密码",httpMethod = "POST")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "newPassWord", value = "新密码", required = true, dataType = "String"),
            @ApiImplicitParam(name = "oldPassWord", value = "旧密码", required = true, dataType = "String"),
    })
    @RequestMapping("/modifypassword")
    public String modifyPassWord(HttpServletRequest httpServletRequest){
        return userService.modifyPassWord(httpServletRequest);
    }
    @ApiOperation(value="模板导出", notes="管理员可导出题库与用户模板用于填写",httpMethod = "POST")
    @ApiImplicitParam(name = "type", value = "模板类型",defaultValue="subject/user", required = true, dataType = "String")
    @RequestMapping("/exporttemplet")
    public String exportTemplet(HttpServletRequest httpServletRequest, Model model, HttpServletResponse httpServletResponse) throws Exception {
        String type=httpServletRequest.getParameter("type");
        if(type.equals("subject")){
            return subjectService.exportSubjectTemplet(httpServletRequest,httpServletResponse);
        }else if(type.equals("user")){
            return userService.exportUserTemplet(httpServletRequest,httpServletResponse);
        }
        return "导出失败";
    }

    @ApiOperation(value="数据导回", notes="管理员可将填写完的模板数据导回",httpMethod = "POST")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "file", value = "数据文件", required = true, dataType = "file"),
            @ApiImplicitParam(name = "type", value = "模板类型",defaultValue="subject/user", required = true, dataType = "String")
    })
    @RequestMapping("/importtemplet")
    public  String importTemplet(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse, Model model,@RequestParam("file") MultipartFile file) throws IOException, BiffException {
    String type=httpServletRequest.getParameter("type");
        if(type.equals("subject")){
            return subjectService.importTemplet(file,httpServletRequest);
        }else{
            return userService.importTemplet(file,httpServletRequest);
        }
    }

    @ApiOperation(value="新增收藏", notes="用户主动新增题目收藏",httpMethod = "POST")
    @ApiImplicitParam(name = "subjectId", value = "题目编号", required = true, dataType = "String")
    @RequestMapping("/addcollectsubject")
    public String addCollectSubject(HttpServletRequest httpServletRequest){
        return subjectService.addCollectSubject(httpServletRequest);
    }

    @ApiOperation(value="删除收藏", notes="用户主动删除题目收藏",httpMethod = "POST")
    @ApiImplicitParam(name = "subjectId", value = "题目编号", required = true, dataType = "String")
    @RequestMapping("/deletecollectsubject")
    public String deleteCollectSubject(HttpServletRequest httpServletRequest,Model model){
        return subjectService.deleteCollectSubject(httpServletRequest);
    }

    @ApiOperation(value="删除错题", notes="用户主动删除错题",httpMethod = "POST")
    @ApiImplicitParam(name = "subjectId", value = "题目编号", required = true, dataType = "String")
    @RequestMapping("/deleteerrorsubject")
    public String deleteErrorSubject(HttpServletRequest httpServletRequest,Model model){
        return subjectService.deleteErrorSubject(httpServletRequest);
    }

    @ApiOperation(value="随机答题", notes="用户随机答题功能",httpMethod = "POST")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "carexam", value = "科目几",defaultValue="1/2/3/4", required = true, dataType = "String"),
            @ApiImplicitParam(name = "cartype", value = "车型",defaultValue="C1/C2", required = true, dataType = "String")
    })
    @RequestMapping("/randomexam")
    public String randomExam(HttpServletRequest httpServletRequest,Model model,@RequestParam("carexam") String carexam,@RequestParam("cartype") String cartype){
        return subjectService.selectRandomSubject(httpServletRequest);
    }
}
